package org.fu.crowd.mvc.controller;

import com.github.pagehelper.PageInfo;
import org.fu.crowd.constant.CrowdConstant;
import org.fu.crowd.entity.Admin;
import org.fu.crowd.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;


    /**
     *     根据账号密码查询用户信息
     * @param admin 用户信息对象
     * @return 查询结果   null为没有查询到
     */
//    @PostMapping("/admin/login")
//    public String doLogin(Admin admin, HttpSession session){
//        Admin resultAdmin = adminService.getAdminByAcctAndPsw(admin);
//        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN,resultAdmin);
//        System.out.println("登录进来了");
//        return "redirect:/redirect";
//    }
//
//
//    @GetMapping("/admin/logout")
//    public String doLogout(HttpSession session){
//        session.invalidate();
//        return "redirect:/";
//    }


//===========================用户维护页===============================================================


    /**
     *          去到用户维护页
     *          用户维护页的查询功能
     */
    @RequestMapping("/main/mainuser")
    public String getPageInfo(@RequestParam(value = "keyword",defaultValue = "")String keyword,
                              @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                              Model model)
    {
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
        if (pageNum>pageInfo.getPages()){
            return "redirect:/main/mainuser?pageNum="+pageInfo.getPages();
        }
        model.addAttribute("keyword1",keyword);
        model.addAttribute(CrowdConstant.ATTR_PAGE_INFO,pageInfo);
        System.out.println();
        System.out.println(pageInfo);
        System.out.println();
        return "mainuser";
    }






    /**
     *    删除指定用户数据
     */
    @GetMapping("/main/remove")
    public String deleteUser(@RequestParam("adminId")Integer adminId,
                             @RequestParam(value = "keyword",defaultValue = "")String keyword,
                             @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        adminService.removeUser(adminId);
        return "redirect:/main/mainuser?keyword="+keyword+"&pageNum="+pageNum;
    }




    /**
     *    跳到修改页
     */
    @GetMapping("/main/user/updateUser")
    public String updateUser(@RequestParam("adminId")Integer adminId,
                             Model model){
        Admin admin = adminService.getAdminById(adminId);
        model.addAttribute("admin",admin);
        return "edit";
    }







    /**
     *   新增用户数据
     */
    @PreAuthorize("hasAuthority('user:save')")
    @PostMapping("/user/addAdmin")
    public String saveUser(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/main/mainuser?pageNum="+Integer.MAX_VALUE;
    }








    /**
     *   修改用户数据
     */
    @PostMapping("/main/user/updateUser/update")
    public String update(Admin admin,
                         @RequestParam(value = "keyword",defaultValue = "")String keyword,
                         @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        adminService.update(admin);
        return "redirect:/main/mainuser?keyword="+keyword+"&pageNum="+pageNum;
    }






}
